//var glbToken = ""; //令牌,全局变量，不能同名。document.getElementById('myscript').getAttribute('key');
//var glbActionURL = 'http://103.59.149.116:6198/test/Mobile.ashx'; //var glbActionURL = 'Mobile.ashx';  //全局变量，不能同名.发布到服务器时需改成后面的
// var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy" 
// console.log(serverURL);
// if (serverURL == null) {
// 	alert("请重新填写检验码");
// }

//登录
function login(staffid, password) {
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL + '?action=login&staffid=' + staffid + '&password=' + password;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			//alert('error:' + xhr.statusText);
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//抄表录入
function AddUseWaterData(UserID, WaterID, ReadCodeDate, LastMonthCode, ThisMonthCode, PreCheckCode, AfterCheckCode,
	EstimateTonnage, Offset, glbToken, Memo) {
		var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
		// console.log(serverURL);
	var url = serverURL + '?action=addusewater&UserID=' + UserID + '&WaterID=' + WaterID + '&token=' + glbToken;
	//var WaterTonnage = parseFloat(ThisMonthCode) - parseFloat(LastMonthCode) + parseFloat(PreCheckCode) - parseFloat(AfterCheckCode);
	var json;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		//data: { "UserID": +UserID.toString(), "WaterID": +WaterID.toString(), "ReadCodeDate": +ReadCodeDate.toString(), "LastMonthCode": +LastMonthCode, "ThisMonthCode": +ThisMonthCode, "PreCheckCode": +PreCheckCode, "AfterCheckCode": +AfterCheckCode, "EstimateTonnage": +EstimateTonnage, "Offset": +Offset },
		data: {
			"ReadCodeDate": +ReadCodeDate.toString(),
			"LastMonthCode": +LastMonthCode,
			"ThisMonthCode": +ThisMonthCode,
			"PreCheckCode": +PreCheckCode,
			"AfterCheckCode": +AfterCheckCode,
			"EstimateTonnage": +EstimateTonnage,
			"Offset": +Offset
		},
		success: function(result) {
			//alert(result);
			json = result;
		},
		error: function(xhr) {
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//抄表更新
function UpdateUseWaterData(UserID, WaterID, ReadCodeDate, LastMonthCode, ThisMonthCode, PreCheckCode, AfterCheckCode,
	EstimateTonnage, Offset, glbToken, Memo) {
		var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
		// console.log(serverURL);
	var url = serverURL + '?action=updateusewater&UserID=' + UserID + '&WaterID=' + WaterID + '&token=' + glbToken;
	var json;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		data: {
			"ReadCodeDate": +ReadCodeDate,
			"LastMonthCode": +LastMonthCode,
			"ThisMonthCode": +ThisMonthCode,
			"PreCheckCode": +PreCheckCode,
			"AfterCheckCode": +AfterCheckCode,
			"EstimateTonnage": +EstimateTonnage,
			"Offset": +Offset
		},
		success: function(result) {
			//alert(result);
			json = result;
		},
		error: function(xhr) {
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//取得用户抄表信息
function getUseWaterData(userid, waterid, glbToken) {
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL + '?action=getusewater&userid=' + userid + '&waterid=' + waterid + '&token=' + glbToken;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			//alert('error:' + xhr.statusText);
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//取得用户水表号列表
function getWaterIDList(userid, glbToken) {
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL + '?action=getwaterlist&userid=' + userid + '&token=' + glbToken;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			//alert('error:' + xhr.statusText);
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//取得用水用户信息
function getUserInfo(userid, glbToken) {
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL + '?action=getuserinfo&userid=' + userid + '&token=' + glbToken;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			//alert('error:' + xhr.statusText);
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//取得登录用户信息
function getStaffInfo(staffid, glbToken) {
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL + '?action=getstaffinfo&staffid=' + staffid + '&token=' + glbToken;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			//alert('error:' + xhr.statusText);
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//修改登录用户的密码
function updatePassword(staffid, oldpwd, newpwd, glbToken) {
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL + '?action=updatepass&oldpwd=' + oldpwd + '&newpwd=' + newpwd + '&staffid=' + staffid +
		'&token=' + glbToken;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			//alert('error:' + xhr.statusText);
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//找回密码,其中pid为身份证号，不能为空
function getPassword(staffid, pid) {
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL + '?action=findpass&staffid=' + staffid + '&pid=' + pid;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			//alert('error:' + xhr.statusText);
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//取得登录账号管辖用户信息
function getUserTable(glbToken, page, rows, username, isall) {
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL + '?action=getusertable&token=' + glbToken + '&page=' + page + '&rows=' + rows + '&username=' +
		username + '&isall=' + isall;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//获取当前用户的下一个用户号及水表号
function getNextUser(userid, waterid, glbToken) {
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL + '?action=getnextuser&token=' + glbToken + '&userid=' + userid + '&waterid=' + waterid;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//获取当前用户的上一个用户号及水表号
function getPriorUser(userid, waterid, glbToken) {
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL + '?action=getprioruser&token=' + glbToken + '&userid=' + userid + '&waterid=' + waterid;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//检查令牌是否有效
function checkToken(glbToken) {
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL + '?action=comparetoken&token=' + glbToken;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			//alert('error:' + xhr.statusText);
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//修改用户的一些信息,目前只允许修改用户名、电话、地址、身份证号
function updateUserInfo(userid, fieldname, fieldvalue, glbToken) {
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL + '?action=updateuserinfo&fieldname=' + fieldname + '&fieldvalue=' + fieldvalue + '&userid=' +
		userid + '&token=' + glbToken;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			//alert('error:' + xhr.statusText);
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
//取得URL上的键值
function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]);
	else return null;
}
// 保存抄表位置信息
function saveCoord(staffid, userid, X, Y, Z, glbToken){
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL + '?action=savecoord&staffid=' + staffid + '&userid=' + userid + '&X=' +
		X + '&Y=' + Y + '&Z=' + Z + '&token=' + glbToken;
	$.ajax({
		url: url,
		async: false,
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			//alert('error:' + xhr.statusText);
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}

// 查询已抄表用户
function getMeter(userid, glbToken){
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL;
	$.ajax({
		url: url,
		async: false,
		data:{
			action: 'getmeter',
			userid: userid,
			token: glbToken
		},
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			//alert('error:' + xhr.statusText);
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
// 提交抄表记录
function submitMeter(wrdids, glbToken){
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL;
	$.ajax({
		url: url,
		async: false,
		data:{
			action: 'submitmeter',
			wrdids: wrdids,
			token: glbToken
		},
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}
// 查询用户欠费情况
function getOwefee(userid, top, glbToken){
	var json;
	var serverURL = localStorage.getItem("serverURL") + "/Mobile.ashx"; //"https://www.jxsjcrj.cn/lnhy"
	// console.log(serverURL);
	var url = serverURL;
	$.ajax({
		url: url,
		async: false,
		data:{
			action: 'getowefee',
			userid: userid,
			top: top,
			token: glbToken
		},
		dataType: "json",
		success: function(data) {
			json = data;
		},
		error: function(xhr) {
			json = '{"status":"error","msg":"' + xhr.statusText + '"}';
		}
	});
	return json;
}